using Sage.CloudStorage.Qiniu.Auth;
using Sage.CloudStorage.Qiniu.Models;
using System.Text;

namespace Sage.CloudStorage.Qiniu.Examples
{
    /// <summary>
    /// 七牛云上传策略使用示例类
    /// 展示如何创建和使用不同类型的上传策略
    /// </summary>
    public class UploadPolicyExample
    {
        /// <summary>
        /// 演示基本上传策略的使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        public static void DemoBasicUpload(string accessKey, string secretKey, string bucket)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 方式1：使用工厂类直接创建上传认证提供者
            var authProvider1 = QiniuUploadPolicyFactory.CreateBasicUploadAuth(qiniuKey, bucket);

            // 方式2：手动创建上传策略，然后创建上传认证提供者
            var policy = UploadPolicy.CreateBasicPolicy(bucket);
            var authProvider2 = new QiniuAuthProvider(qiniuKey, policy.ToJson());

            // 使用认证提供者进行上传操作...
            // 实际使用时，将authProvider传递给HTTP客户端进行认证
        }

        /// <summary>
        /// 演示指定资源键的上传策略使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        /// <param name="key">资源键名</param>
        public static void DemoKeyUpload(string accessKey, string secretKey, string bucket, string key)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 方式1：使用工厂类直接创建上传认证提供者（允许覆盖）
            var authProvider1 = QiniuUploadPolicyFactory.CreateKeyUploadAuth(qiniuKey, bucket, key);

            // 方式2：使用工厂类创建不允许覆盖的上传认证提供者
            var authProvider2 = QiniuUploadPolicyFactory.CreateKeyUploadAuth(qiniuKey, bucket, key, insertOnly: true);

            // 方式3：手动创建上传策略，然后创建上传认证提供者
            var policy = UploadPolicy.CreateKeyPolicy(bucket, key, insertOnly: true);
            var authProvider3 = new QiniuAuthProvider(qiniuKey, policy.ToJson());

            // 使用认证提供者进行上传操作...
        }

        /// <summary>
        /// 演示前缀上传策略的使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        /// <param name="keyPrefix">资源键前缀</param>
        public static void DemoPrefixUpload(string accessKey, string secretKey, string bucket, string keyPrefix)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 方式1：使用工厂类直接创建上传认证提供者
            var authProvider1 = QiniuUploadPolicyFactory.CreatePrefixUploadAuth(qiniuKey, bucket, keyPrefix);

            // 方式2：手动创建上传策略，然后创建上传认证提供者
            var policy = UploadPolicy.CreatePrefixPolicy(bucket, keyPrefix);
            var authProvider2 = new QiniuAuthProvider(qiniuKey, policy.ToJson());

            // 使用认证提供者进行上传操作...
        }

        /// <summary>
        /// 演示自定义上传策略的使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        public static void DemoCustomUpload(string accessKey, string secretKey)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 创建自定义上传策略
            var policy = new UploadPolicy
            {
                // 必填字段
                Scope = "your-bucket:your-key.jpg",
                Deadline = (uint)DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds(),

                // 可选字段
                InsertOnly = 1, // 仅允许新增，不允许覆盖
                ReturnBody = "{\"key\":$(key),\"hash\":$(etag),\"size\":$(fsize),\"width\":$(imageInfo.width),\"height\":$(imageInfo.height)}", // 自定义返回内容
                FsizeLimit = 10485760, // 限制文件大小最大为10MB
                MimeLimit = "image/jpeg;image/png", // 只允许上传JPG和PNG图片
                FileType = 0, // 标准存储
                DetectMime = 1 // 开启MimeType侦测
            };

            // 创建上传认证提供者
            var authProvider = QiniuUploadPolicyFactory.CreateCustomUploadAuth(qiniuKey, policy);

            // 使用认证提供者进行上传操作...
        }

        /// <summary>
        /// 演示带回调的上传策略使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        /// <param name="callbackUrl">回调URL</param>
        public static void DemoCallbackUpload(string accessKey, string secretKey, string bucket, string callbackUrl)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 创建基本上传策略
            var policy = UploadPolicy.CreateBasicPolicy(bucket);

            // 设置回调相关参数
            policy.CallbackUrl = callbackUrl;
            policy.CallbackBody = "{\"key\":$(key),\"hash\":$(etag),\"size\":$(fsize),\"mimeType\":$(mimeType)}"; // JSON格式回调内容
            policy.CallbackBodyType = "application/json"; // 设置回调内容类型为JSON

            // 创建上传认证提供者
            var authProvider = new QiniuAuthProvider(qiniuKey, policy.ToJson());

            // 使用认证提供者进行上传操作...
        }

        /// <summary>
        /// 演示带数据处理的上传策略使用
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        /// <param name="saveBucket">处理结果保存的存储空间</param>
        /// <param name="notifyUrl">处理结果通知URL</param>
        public static void DemoPersistentOpsUpload(string accessKey, string secretKey, string bucket, string saveBucket, string notifyUrl)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 创建基本上传策略
            var policy = UploadPolicy.CreateBasicPolicy(bucket);

            // 设置数据处理相关参数
            // 示例：上传视频后自动转码为MP4和FLV格式
            string saveKeyMp4 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{saveBucket}:$(key)_mp4.mp4"));
            string saveKeyFlv = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{saveBucket}:$(key)_flv.flv"));

            policy.PersistentOps = $"avthumb/mp4|saveas/{saveKeyMp4};avthumb/flv|saveas/{saveKeyFlv}";
            policy.PersistentNotifyUrl = notifyUrl;
            policy.PersistentPipeline = "your-pipeline"; // 可选，指定转码队列

            // 创建上传认证提供者
            var authProvider = new QiniuAuthProvider(qiniuKey, policy.ToJson());

            // 使用认证提供者进行上传操作...
        }

        /// <summary>
        /// 演示高级上传策略的使用（使用新增的CreateAdvancedUploadAuth方法）
        /// </summary>
        /// <param name="accessKey">七牛云AccessKey</param>
        /// <param name="secretKey">七牛云SecretKey</param>
        /// <param name="bucket">存储空间名称</param>
        public static void DemoAdvancedUpload(string accessKey, string secretKey, string bucket)
        {
            // 创建七牛云密钥对象
            var qiniuKey = new QiniuKey(accessKey, secretKey);

            // 示例1：创建带文件大小限制和MIME类型限制的上传认证
            var authProvider1 = QiniuUploadPolicyFactory.CreateAdvancedUploadAuth(
                qiniuKey: qiniuKey,
                bucket: bucket,
                fsizeLimit: 10485760, // 10MB
                mimeLimit: "image/jpeg;image/png",
                returnBody: "{\"key\":$(key),\"hash\":$(etag),\"size\":$(fsize),\"mimeType\":$(mimeType)}"
            );

            // 示例2：创建带指定键名和回调的上传认证
            var authProvider2 = QiniuUploadPolicyFactory.CreateAdvancedUploadAuth(
                qiniuKey: qiniuKey,
                bucket: bucket,
                key: "path/to/image.jpg",
                insertOnly: true,
                callbackUrl: "https://your-server.com/qiniu/callback",
                callbackBody: "{\"key\":$(key),\"hash\":$(etag),\"size\":$(fsize)}"
            );

            // 示例3：创建带前缀的上传认证
            var authProvider3 = QiniuUploadPolicyFactory.CreateAdvancedUploadAuth(
                qiniuKey: qiniuKey,
                bucket: bucket,
                keyPrefix: "uploads/images/",
                fsizeLimit: 5242880, // 5MB
                mimeLimit: "image/*"
            );

            // 使用认证提供者进行上传操作...
        }
    }
}